package cn.com.aiouyi.service.impl;

import cn.com.aiouyi.dao.ArticleDao;
import cn.com.aiouyi.entity.Accessory;
import cn.com.aiouyi.entity.Article;
import cn.com.aiouyi.entity.ArticleClass;
import cn.com.aiouyi.service.AccessoryService;
import cn.com.aiouyi.service.ArticleService;
import cn.com.aiouyi.util.AssertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Created by tommy on 2018/4/11.
 */
@Transactional
@Service(value = "articleServiceImpl")
public class ArticleServiceImpl extends BaseServiceImpl<Article, Long> implements ArticleService {

    @Resource(name = "articleDaoImpl")
    private ArticleDao articleDao;

    @Resource(name = "accessoryServiceImpl")
    private AccessoryService accessoryServiceImpl;

    @Override
    public Article findExist(ArticleClass articleClass, String title, String author) {

        return articleDao.findExist(articleClass, title, author);
    }

    @Override
    public void delete(Long[] ids){
        AssertUtils.notNull(ids);
        AssertUtils.isTrue(ids.length > 0);
        for (int i = 0; i < ids.length; i++) {
            Article article = this.find(ids[i]);
            if (article != null){
                if ( article.getMainPic() != null ) {
                    accessoryServiceImpl.delete(article.getMainPic());
                }
                this.realDelete(article);
            }
        }
    }
}
